package test.h0820;

import java.util.*;

public class Main5 {
    static class TreeNode {
        char val;
        TreeNode left;
        TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String postOrder = sc.next();
        String inOrder=sc.next();

        TreeNode root = buildTree(postOrder, inOrder);

        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.offerLast(root);
        List<Character> res=new ArrayList<>();
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode currentNode = queue.removeFirst();
                res.add(currentNode.val);
                if (currentNode.left!=null){
                    queue.offerLast(currentNode.left);
                }
                if (currentNode.right!=null){
                    queue.offerLast(currentNode.right);
                }
            }
        }
        StringBuilder builder = new StringBuilder();
        for (Character c : res) {
            builder.append(c);
        }
        System.out.println(builder);
    }

    private static TreeNode buildTree(String postOrder, String inOrder) {
        if (postOrder.isEmpty()||inOrder.isEmpty()){
            return null;
        }
        return buildTreeHelper(postOrder.toCharArray(),inOrder.toCharArray());
    }

    private static TreeNode buildTreeHelper(char[] postOrder, char[] inOrder) {
        //判空
        if (postOrder.length == 0 || inOrder.length == 0) {
            return null;
        }
        int len = postOrder.length;
        char rootVal = postOrder[len - 1];
        TreeNode root=new TreeNode(rootVal);

        int mid=0;
        //找到根节点
        for (int i = 0; i < len; i++) {
            if (postOrder[len-1]==inOrder[i]){
                mid=i;
                break;
            }
        }
        //构建左子树中序和后序序列
        char[] leftInOrder= Arrays.copyOfRange(inOrder,0,mid);
        char[] leftPostOrder=Arrays.copyOfRange(postOrder,0,mid);

        char[] rightInOrder=Arrays.copyOfRange(inOrder,mid+1,len);
        char[] rightPostOrder=Arrays.copyOfRange(postOrder,mid,len-1);
        root.left=buildTreeHelper(leftPostOrder,leftInOrder);
        root.right=buildTreeHelper(rightPostOrder,rightInOrder);

        return root;
    }
}
